angular.module('goods-list', ['swipe-list','dropdown-menu','ngStorage','share-model'])

.controller 'GoodsListCtrl', ($scope, $state, $http,showToast, $ionicHistory, $stateParams, goodsInfo, wfCache,$ionicViewSwitcher,userData,$ionicPopup,paginationFactory,$ionicScrollDelegate,dropdownMenu,$localStorage,shareModel) ->
    paginationSrv = new paginationFactory('GoodsListCtrl','/updshop/')
    $scope.$storage = $localStorage
    $scope.plglText = '批量管理'

    dropdownMenu.set [
        {id:'0',text: '出售中', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'1'
            stype:1
            showtype:0
        })
        }
        {id:'1',text: '未上架', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'0'
            stype:1
            showtype:0
        })
        }
        {id:'2',text: '最新上架', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'1'
            stype:1
            showtype:1
        })
        }
        {id:'3',text: '热门促销', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'1'
            stype:1
            showtype:2
        })
        }
        {id:'4',text: '店长推荐', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'1'
            stype:1
            showtype:3
        })
        }
        {id:'5',text: '分类管理', func: -> $state.go 'tab.sort-manage'
        }
    ]

    shareModel.set [
        {img:'img/timeline.png',text:'分享到朋友圈',func: -> $scope.goShare0()}
        {img:'img/wechat.png',text:'分享给朋友',func: -> $scope.goShare1()}
    ]


    $scope.goShare0 = ->
        $http.get('/seller/',params:{action:'shareproduct',shopid:userData.getToken(),proid:$scope.proid})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.TIMELINE
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '网络异常,请检查网络是否连接'

    $scope.goShare1 =  ->
        $http.get('/seller/',params:{action:'shareproduct',shopid:userData.getToken(),proid:$scope.proid})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.SESSION
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '网络异常,请检查网络是否连接'


    unWatch = paginationSrv.watch (date) ->
        $scope.goodsList = _.map date,(item)->
            goods = {
                id:item.id
                img:item.img
                txt1:item.title
                txt2:"销售价:¥#{item.price}"
                upkproid:item.upkproid
            }
        if $scope.checkContent and $scope.goodsList?.length==0
            $scope.nocontent=true
        else
            $scope.nocontent=false

    _.extend $scope,
        showMenu: (e) ->
            dropdownMenu.show e
        canLoadMore: -> paginationSrv.canLoadMore()
        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()
        goGoodsSearch:(currentId) ->
            $state.go 'tab.goods-search-home', currentItemId:currentId
        pick: (i,currentItem) ->
            console.log 'pick',i
            console.log currentItem
            if currentItem != '1'
                goodsInfo.isOnLine = true
                $state.go 'tab.goods-detail', goodId: $scope.goodsList[i].id
            else if currentItem == '1'
                goodsInfo.isOnLine = false
                $state.go 'tab.goods-detail', goodId: $scope.goodsList[i].id
        remove: (i) ->
            $http.post('/updshop/',{action:'delproduct',userid:userData.getToken(),id:$scope.goodsList[i].id})
            .success (resp) ->
                if resp?.status == '0'
                    showToast '删除成功'
                    _.each $localStorage.tabMarketGoodList,(item)->
                        if ''+item.id == ''+$scope.goodsList[i].upkproid
                            item.isadd = 0
                    $scope.goodsList.splice(i,1)
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
        share:(i,e) ->
            $scope.proid = $scope.goodsList[i].id
            shareModel.show e
        gogoodsadd: ->
            $state.go 'tab.goods-add'
        takeOff: ->
            goodsToDownLoad = []
            ids=''
            _.each $scope.goodsList,(item)->
                if item.isCheckIn == true
                    goodsToDownLoad.push item
                    ids += item.id+','
            ids = ids.slice(0,ids.length-1)
            $http.post('/updshop/',{action:'proshelves',userid:userData.getToken(),id:ids,issale:'0'})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.goodsList = _.difference $scope.goodsList,goodsToDownLoad
                    $localStorage['dmCurrentItem'].func()
                    showToast '下架成功'
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
        gogoodsManage:(i) ->
            if i == '1'
                $scope.needOn = !$scope.needOn
            else
                $scope.needDown = !$scope.needDown
            if $scope.needDown or $scope.needOn
                $scope.plglText = '取消管理'
            else
                $scope.plglText = '批量管理'
        batOnLine: ->
            goodsToUpLoad = []
            ids=''
            _.each $scope.goodsList,(item)->
                if item.isCheckIn == true
                    goodsToUpLoad.push item
                    ids += item.id+','
            ids = ids.slice(0,ids.length-1)
            $http.post("/updshop/",{action:'proshelves',userid:userData.getToken(),id:ids,issale:'1'})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.goodsList = _.difference $scope.goodsList,goodsToUpLoad
                    $localStorage['dmCurrentItem'].func()
                    showToast '上架成功'
                else
                    showToast '没有该商品'
            .error ->
                showToast '您的网络又调皮了'
    $scope.$on '$destory', ->
        unWatch()
    $scope.$on '$ionicView.enter', ->
        $scope.checkContent=true
        $scope.needDown = false
        $scope.needOn = false
        $scope.$storage.dmCurrentItem = {id:'0',text: '出售中', func: -> paginationSrv.reload(true, {
            action:'productlist'
            userid: userData.getToken()
            warehouse:'1'
            stype:1
            showtype:0
        })
        }
        $scope.$storage.dmCurrentItem.func()
        $ionicScrollDelegate.scrollTop()

